package com.xiaodou.mjf.web.xface.product.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaodou.mjf.application.core.service.LoanService;
import com.xiaodou.mjf.entity.core.Loan;
import com.xiaodou.mjf.infrastructure.common.controller.ApiController;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;
import com.xiaodou.mjf.infrastructure.common.utils.StringUtils;
import com.xiaodou.mjf.web.xface.common.ResultCode;

/**
 * Created by Rocky on 3/3/15.
 */
@RestController
@RequestMapping("v1/product")
public class SubmitStageController extends ApiController {
	@Resource
	LoanService loanService;

	/**
	 * @param 客户编号
	 *            uid
	 * @param 月收入
	 *            monthly_income
	 * @param 医疗机构名称
	 *            organization_name
	 * @param 医疗结构联系方式
	 *            organization_phone
	 * @param 借款金额
	 *            loan_amount
	 * @param 借款期限term
	 * @param 贷款项目描述
	 *            description
	 */
	@RequestMapping("submit")
	public Map<String, Object> submit(
			@RequestHeader(required = false) String uid,
			@RequestParam(value = "monthly_income", required = false) Double monthlyIncome,
			@RequestParam(value = "organization_name", required = false) String organizationName,
			@RequestParam(value = "project_name", required = false) String projectName,
			@RequestParam(value = "organization_phone", required = false) String organizationPhone,			
			@RequestParam(value = "loan_amount", required = false) Double loanAmount,
			@RequestParam(value = "down_payment_deposit", required = false) Double downpaymentDeposit,
			Integer term) {
		if (StringUtils.isBlank(uid) || StringUtils.isBlank(organizationName)
				|| StringUtils.isBlank(organizationPhone)|| monthlyIncome==null|| loanAmount==null|| downpaymentDeposit==null) {
			return failure(ResultCode.MISSING_PARAMS);
		}
		Map<String,Object> result = success();
		Loan loan = new Loan();

		// 客户编号
		loan.setMemberId(uid);
		// 月收入
		loan.setMonthlyIncome(monthlyIncome);
		
		try {
			// 机构名称
			loan.setOrganizationName(URLDecoder.decode(organizationName, "UTF-8"));
			//项目名称
			loan.setProjectName(URLDecoder.decode(projectName, "UTF-8"));
		} catch (UnsupportedEncodingException e1) {			
			e1.printStackTrace();
		}		
		// 机构电话
		loan.setOrganizationPhone(organizationPhone);
		// 期数
		loan.setTerm(term);
		// 贷款金额
		loan.setLoanAmount(loanAmount);
		// 首付金额
		loan.setDownPaymentDeposit(downpaymentDeposit);
		//项目金额
		loan.setProjectAmount(loanAmount+downpaymentDeposit);
		//交易时间
		loan.setTradeDate(DateUtils.getCurrentTime());
		//申请首付日期
		if(downpaymentDeposit.doubleValue()!=0D){
			loan.setApplyDate(loan.getTradeDate());
		}
		//产品id "200001"
		loan.setProductId("200001");
		//未使用
		loan.setFlag(Loan.FLAG_NOTUSE);
		loan.setStatus(Loan.WAIT_AUDIT);
		//默认值初始为0
		loan.setFinanceStatus(0);

		try {
			loanService.save(loan);
		} catch (Exception e) {
			logger.error(e.toString());
			return failure(ResultCode.SUBMIT_INSERT_ERROR);
		}

		// 订单号 
//		result.put("orderNo", "10000");

		return result;
	}

}
